//
//  AppDelegate.swift
//  weibo
//
//  Created by Apple on 16/3/7.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var observer: NSObjectProtocol?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //  测试解档对象是否成功
//        let user = CZUserAccount.userAccount()
//        print(user)
        
        //  监听通知
//        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVc", name: CZSwitchRootVCNotifation, object: nil)
        
        //  监听通知2
        observer = NSNotificationCenter.defaultCenter().addObserverForName(CZSwitchRootVCNotifation, object: nil, queue: nil) { [weak self] (noti) -> Void in
    
            //  切换更试图控制器
            let vc = noti.object is CZOAuthViewController
            if vc {
                self?.window?.rootViewController = CZWelcomeViewController()
            } else {
                self?.window?.rootViewController = CZMainViewController()
            }
            
            
        }

        //  1. 初始化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //  2. 设置跟视图控制器
        window?.rootViewController = CZUserAccountViewModel.sharedAccount.userLogin ? showLoginVC() : CZMainViewController()
        
        
        
        //window?.rootViewController = CZMainViewController() //UINavigationController(rootViewController: CZOAuthViewController())//CZMainViewController()
        //  3. 设置主窗口并显示
        window?.makeKeyAndVisible()
        
        return true
    }
    
    //  显示登录的视图控制器
    func showLoginVC() -> UIViewController {
        if hasNewVersion() {
            //  新版本则显示新特性页面
            return CZNewfeatureViewController()
        } else {
            //  否则显示欢迎页面
            return CZWelcomeViewController()
        }
    }
    
    // 如果有新版本，显示新特性页，否则显示欢迎也
    func hasNewVersion() -> Bool {
        let saveVersion = NSUserDefaults.standardUserDefaults().stringForKey("version")
        
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String
        
        //  保存当前的版本,如果当前的版本跟值保存的版本不一样，保存最新的版本
        if let lastVersion = currentVersion where lastVersion != saveVersion {
            //  版本不一样保存最新版本号
            NSUserDefaults.standardUserDefaults().setObject(lastVersion, forKey: "version")
            NSUserDefaults.standardUserDefaults().synchronize()
            
            if let localVersion = saveVersion {
                //  如果最新的版本跟保存的版本成降许，说明是新版本
                if lastVersion.compare(localVersion) == NSComparisonResult.OrderedDescending {
                    return true
                }
            } else {
                return true
            }
            
        }
        
        return false
    }

   
//    @objc private func switchRootVc() {
//        
//        let welcomeVC = CZWelcomeViewController()
//        window?.rootViewController = welcomeVC
//    
//    }
    
    deinit {
//        NSNotificationCenter.defaultCenter().removeObserver(self)
        NSNotificationCenter.defaultCenter().removeObserver(observer!)
    }

}

